#!/bin/bash
# vi: set ft=sh:

set -e

# This is hardcoded in configure-helix-p4d.rsh :(
P4PASSWD="${P4PASSWD:-pass12349ers!}"

for DIR in $P4ROOT $P4SSLDIR; do
    mkdir -m 0700 -p $DIR
    chown perforce:perforce $DIR
done

if ! p4dctl list 2>/dev/null | grep -q $P4NAME; then
    /opt/perforce/sbin/configure-helix-p4dr.sh $P4NAME -n -p $P4PORT -r $P4ROOT -u $P4USER -P "${P4PASSWD}"
fi

#if [[ $(ls $P4SSLDIR/* | wc -l) = 0 ]]; then
#    $P4D -Gc
#fi

exec su -s /bin/sh -c 'exec "$0" "$@"' perforce -- /usr/sbin/p4d -r $P4ROOT -p $P4PORT
